package com.bm.beautifulmouse.effect;

import com.bm.beautifulmouse.model.EffectConfig;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.Timeline;
import javafx.scene.layout.Pane;
import javafx.scene.shape.SVGPath;
import javafx.util.Duration;
import java.util.Random;

public class HeartEffect implements Effect {
    private final Random random = new Random();

    @Override
    public void generate(Pane container, double x, double y, EffectConfig config) {
        for (int i = 0; i < config.getParticleCount(); i++) {
            SVGPath heart = createHeart(config.getSize());
            heart.setFill(config.getColor());
            
            // 设置心形的初始位置在鼠标点击处
            double startX = x - (heart.getBoundsInLocal().getWidth() / 2);
            double startY = y - (heart.getBoundsInLocal().getHeight() / 2);
            heart.setLayoutX(startX);
            heart.setLayoutY(startY);

            // 计算随机的扩散方向和距离
            double angle = (360.0 / config.getParticleCount()) * i + random.nextDouble() * 30;
            double distance = (30 + random.nextDouble() * 20) * config.getRange();
            double duration = 1.5 / config.getSpeed();

            // 计算目标位置
            double targetX = startX + Math.cos(Math.toRadians(angle)) * distance;
            double targetY = startY + Math.sin(Math.toRadians(angle)) * distance;

            // 创建动画
            Timeline timeline = new Timeline(
                    new KeyFrame(Duration.ZERO,
                            new KeyValue(heart.layoutXProperty(), startX),
                            new KeyValue(heart.layoutYProperty(), startY),
                            new KeyValue(heart.opacityProperty(), 1.0),
                            new KeyValue(heart.scaleXProperty(), 0.2),
                            new KeyValue(heart.scaleYProperty(), 0.2)
                    ),
                    new KeyFrame(Duration.seconds(duration / 2),
                            new KeyValue(heart.scaleXProperty(), 1.0),
                            new KeyValue(heart.scaleYProperty(), 1.0)
                    ),
                    new KeyFrame(Duration.seconds(duration),
                            new KeyValue(heart.layoutXProperty(), targetX),
                            new KeyValue(heart.layoutYProperty(), targetY),
                            new KeyValue(heart.opacityProperty(), 0.0),
                            new KeyValue(heart.scaleXProperty(), 0.5),
                            new KeyValue(heart.scaleYProperty(), 0.5)
                    )
            );

            timeline.setOnFinished(e -> container.getChildren().remove(heart));
            container.getChildren().add(heart);
            timeline.play();
        }
    }

    private SVGPath createHeart(double size) {
        SVGPath heart = new SVGPath();
        // 更新为标准的心形SVG路径
        heart.setContent("M 0 -7.5 C -10 -22.5 -30 -7.5 0 12.5 C 30 -7.5 10 -22.5 0 -7.5");
        
        // 缩放到合适的大小
        double scale = size * 1.5;
        heart.setScaleX(scale);
        heart.setScaleY(scale);
        
        return heart;
    }
}